package com.esunny.online.service.impl;

import com.esunny.online.dao.ResourceDao;
import com.esunny.online.dao.ResourceTagDao;
import com.esunny.online.dao.TagDao;
import com.esunny.online.domain.dto.ResourceListQuery;
import com.esunny.online.domain.entity.Resource;
import com.esunny.online.domain.entity.ResourceTag;
import com.esunny.online.domain.entity.Tag;
import com.esunny.online.domain.entity.User;
import com.esunny.online.service.ResourceService;
import com.esunny.online.util.SpringBeanUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：BCC
 * @date ：Created in 2020/7/18 8:42
 * @description： resourceService的实现类
 * @modified By：BCC
 * @version: $ 2.0.1
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @javax.annotation.Resource
    private ResourceDao resourceDao;

    /**
     * 查找未被审核的资源
     * @param resourceListQuery 查找条件
     * @return 未审核通过的资源
     */
    @Override
    public List<Resource> selectUncheckResource(ResourceListQuery resourceListQuery){
        return resourceDao.selectUncheckResource(resourceListQuery);
    }

    /**
     * 查找未被审核的资源数量
     * @param resourceListQuery 查找条件
     * @return 未审核通过的资源数量
     */
    @Override
    public int selectUncheckCount(ResourceListQuery resourceListQuery){
        return resourceDao.selectUncheckCount(resourceListQuery);
    }

    /**
     * 根据Id查找resource
     *
     * @param id resourceId
     * @return Resource对象
     */
    @Override
    public Resource getResourceById(int id) {
        return  resourceDao.getResourceById(id);
    }

    /**
     * 更新资源信息
     * @param resource 资源信息
     * @param user 用户信息
     * @return 是否更新成功
     */
    @Override
    public boolean updateResource(Resource resource, User user){
        // 判断用户级别
        int level = user.getLevel();
        // 普通用户修改修改资源状态为待审核：0
        if (level == 0){
            resource.setStatus(0);
        }else{
            // 有利于判断状态
            if(resource.getStatus() == 0){
                resource.setStatus(-1);
            }
        }
        if (resource.getTags()!=null&&resource.getTags()!=""){
            // 获取bean对象
            ResourceTagDao resourceTagDao = SpringBeanUtil.getBean(ResourceTagDao.class);
            // 根据资源id 删除资源-标签记录
            ResourceTag resourceTag1 = new ResourceTag();
            resourceTag1.setResourceid(resource.getId());
            resourceTagDao.deleteResourceTag(resourceTag1);
            // 获取bean对象
            TagDao tagDao = SpringBeanUtil.getBean(TagDao.class);
            for (String tagid: resource.getTags().split(",")) {
                // 设置查找标签条件
                Tag tag = new Tag();
                tag.setId(Integer.parseInt(tagid));
                Tag tagByQuery = tagDao.getTagByQuery(tag);
                // 创建资源-标签数据
                ResourceTag resourceTag = new ResourceTag();
                resourceTag.setResourceid(resource.getId());
                resourceTag.setTagid(Integer.parseInt(tagid));
                resourceTag.setTagname(tagByQuery.getName());
                resourceTag.setIsdelete(false);
                // 添加记录
                resourceTagDao.addResourceTag(resourceTag);
            }


        }
        // 判断是否更新成功
        int row = resourceDao.updateResource(resource);

        return row == 1;
    }

    /**
     * 通过标签，资源id和查询数目来查找相关资源列表
     * @param query 查询条件
     * @return 相关资源信息
     */
//    @Override
    public List<Resource> testqueryRelatedResource(Map<String, Object> query) {
        // 获取资源标签
        String tag = (String) query.get("tags");
        // 获取查询条数
        Integer count = (Integer) query.get("count");
        // 获取当前资源文件id
        Integer resourceid = (Integer) query.get("resourceid");
        // 获取全部资源数据
        List<Resource> resourceAll = resourceDao.selectAll();
        // 存储相关资源列表
        List<Resource> resources = new ArrayList<>();
        // 循环遍历全部资源查找相关资源
        int number = 0;
        for (Resource value : resourceAll) {
            String tags = value.getTags();
            if (value.getStatus() == 2 && tags != null && !"".equals(tags) && tags.contains(tag)) {
                if (value.getId() != resourceid) {
                    resources.add(value);
                    number++;
                }
            }
            if (number >= count) {
                break;
            }
        }

        return resources;
    }

    /**
     * 通过标签，资源id和查询数目来查找相关资源列表
     * @param query 查询条件
     * @return 相关资源信息
     */
    @Override
    public List<Resource> queryRelatedResource(Map<String, Object> query) {
        // 获取资源标签
        String tag = (String) query.get("tags");
        // 获取查询条数
        Integer count = (Integer) query.get("count");
        // 获取当前资源文件id
        Integer resourceid = (Integer) query.get("resourceid");
        // 获取当前资源信息
        Resource resource = resourceDao.getResourceById(resourceid);
        // 获取全部资源数据
        List<Resource> resourceAll = resourceDao.selectAll();

        ResourceTagDao resourceTagDao = SpringBeanUtil.getBean(ResourceTagDao.class);
        List<ResourceTag> resourceTags = resourceTagDao.getResourceTagByTagid(resource.getTags().split(","));
        System.out.println(resourceTags);

        // 存储相关资源列表
        List<Resource> resources = new ArrayList<>();
        // 循环遍历全部资源查找相关资源
        int number = 0;
        for (Resource resource1: resourceAll) {
            for (ResourceTag resourceTag1:resourceTags) {
                if (resource1.getStatus() == 2 && resource1.getId() == resourceTag1.getResourceid() && resource1.getId() != resourceid){
                    boolean flag = true;
                    for (Resource resource2:resources) {
                        if (resource1.getId() != resource2.getId()){
                            flag = true;
                        }else{
                            flag = false;
                        }
                    }
                    if (flag){
                        resources.add(resource1);
                        number++;
                    }
                }
                if (number >= count) {
                    break;
                }
            }
            if (number >= count) {
                break;
            }
        }

        return resources;
    }

    /**
     * 上传文件功能
     *
     * @param resource 数据
     * @return 主键
     */
    @Override
    public int addResource(Resource resource, User user) {
        if (resource == null) {
            throw new IllegalArgumentException("参数不能为空");
        } else {
            resource.setCreatedtime(new Date());
            resource.setUsername(user.getUsername());
            resource.setCreatedby(user.getRealname());
            int level = user.getLevel();
            if (level == 1){
                resource.setStatus(2);
            }
            return resourceDao.addResource(resource);
        }
    }

    /**
     * 查询热点资源
     * @param pageSize 查询条数
     * @return 热点资源列表
     */
    @Override
    public List<Resource> queryHotResource(int pageSize){
        return resourceDao.queryHotResource(pageSize);
    }

    /**
     * 分页查询
     *
     * @param resourceListQuery 数据
     * @return 资源列表+资源总数
     */
    @Override
    public Map<String, Object> queryByLimit( ResourceListQuery resourceListQuery) {
        List<Resource> resourceList = new ArrayList<>();
        // 根据标签条件获取列表
        if (resourceListQuery.getTags() != null && !"".equals(resourceListQuery.getTags())){
            List<Resource> resourceAll = resourceDao.selectAll();
            List<Resource> resources = new ArrayList<>();
            for (Resource value : resourceAll) {
                String tags = value.getTags();
                if (value.getStatus() == 2 && tags != null && !"".equals(tags)) {
                    for (String tagid: tags.split(",")) {
                        if (tagid.equals(resourceListQuery.getTags())){
                            resources.add(value);
                            break;
                        }
                    }
                }
            }
            resourceListQuery.setOffset();
            for (int i = resourceListQuery.getOffset();i<resourceListQuery.getOffset()+resourceListQuery.getPageSize();i++){
                if(i < resources.size()){
                    resourceList.add(resources.get(i));
                }
            }
        }else{
            // 标签不是筛选条件时获取列表
            resourceListQuery.setOffset();
            resourceList = resourceDao.selectResource(resourceListQuery);
        }

        Map<String, Object> map = new HashMap<>(0);
        map.put("resourceList", resourceList);

        return map;
    }

    /**
     * 查询数据总数
     *
     * @return 数据总数
     */
    @Override
    public int queryCount(ResourceListQuery resourceListQuery) {
        int count = 0;
        // 标签作为条件
        if (resourceListQuery.getTags() != null && !"".equals(resourceListQuery.getTags())){
            List<Resource> resourceAll = resourceDao.selectAll();
            for (Resource resource : resourceAll) {
                String tags = resource.getTags();
                if (resource.getStatus() == 2 && tags != null && !"".equals(tags)) {
                    for (String tagid: tags.split(",")) {
                        if (tagid.equals(resourceListQuery.getTags())){
                            count++;
                            break;
                        }
                    }
                }
            }
        }else {
            // 没有标签作为条件
            count = resourceDao.selectCount(resourceListQuery);
        }

        return count;
    }

    /**
     * 删除功能
     *
     * @param id 数据
     * @return 受影响行数
     */
    @Override
    public int deleteResource(int id) {
        return resourceDao.deleteResource(id);
    }


}
